package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Test2 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test2.class.getResource(".").toURI());
        System.out.println(dir);
        String pname = Test2.class.getPackage().getName();
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File sub : subs) {
//            String[] s= sub.getName().split("\\.",2);
//            String s1 = s[0];
//            Class cls1 = Class.forName(pname+"."+s1);
            String filename = sub.getName();
//            String className = filename.substring(0,filename.lastIndexOf("."));
            String className = filename.replaceAll(".class", "");
            filename = pname + "." + className;
            Class cls1 = Class.forName(filename);
            Object obj = cls1.newInstance();
            System.out.println(obj);

            Method[] methods = cls1.getDeclaredMethods();
            for (Method method : methods) {
                if (method.getParameterCount() == 0 && method.getModifiers() == Modifier.PUBLIC) {
                    System.out.println(method.getName());
                    method.invoke(obj);
                }
            }
        }
    }
}